#include "icon.h"
#include <QPainter>

Icon::Icon(const QString &fileName, const QColor &color):
    mFile(fileName),
    mColor(color)
{
//    addFile(fileName, QSize(), QIcon::Normal, QIcon::On);
    QImage img(fileName);

    // normal
    QImage normalImg = img;
    for(int y = 0; y < img.height(); y++) {
        for(int x = 0; x < img.width(); x++) {
            QColor color = img.pixelColor(x, y);
            if(color.alpha() == 0)
                continue;
            QColor drawColor = mColor;
            drawColor.setAlpha(color.alpha());
            normalImg.setPixelColor(x, y, drawColor);
        }
    }
    addPixmap(QPixmap::fromImage(normalImg), QIcon::Normal, QIcon::On);

    // disable
    QImage disableImg = img;
    for(int y = 0; y < img.height(); y++) {
        for(int x = 0; x < img.width(); x++) {
            QColor color = img.pixelColor(x, y);
            if(color.alpha() == 0)
                continue;
            QColor drawColor(100, 100, 100);
            drawColor.setAlpha(color.alpha());
            disableImg.setPixelColor(x, y, drawColor);
        }
    }
    addPixmap(QPixmap::fromImage(disableImg), QIcon::Disabled, QIcon::On);

    // active
    QImage activeImg = img;
    for(int y = 0; y < img.height(); y++) {
        for(int x = 0; x < img.width(); x++) {
            QColor color = img.pixelColor(x, y);
            if(color.alpha() == 0)
                continue;
            QColor drawColor = mColor.lighter(120);
            drawColor.setAlpha(color.alpha());
            activeImg.setPixelColor(x, y, drawColor);
        }
    }
    addPixmap(QPixmap::fromImage(activeImg), QIcon::Active, QIcon::On);

    // select
    QImage selectImg = img;
    for(int y = 0; y < img.height(); y++) {
        for(int x = 0; x < img.width(); x++) {
            QColor color = img.pixelColor(x, y);
            if(color.alpha() == 0)
                continue;
            QColor drawColor = mColor.lighter(130);
            drawColor.setAlpha(color.alpha());
            selectImg.setPixelColor(x, y, drawColor);
        }
    }
    addPixmap(QPixmap::fromImage(selectImg), QIcon::Selected, QIcon::On);

    // press
    mPressedImg = img;
    for(int y = 0; y < img.height(); y++) {
        for(int x = 0; x < img.width(); x++) {
            QColor color = img.pixelColor(x, y);
            if(color.alpha() == 0)
                continue;
            QColor drawColor = mColor.lighter(80);
            drawColor.setAlpha(color.alpha());
            mPressedImg.setPixelColor(x, y, drawColor);
        }
    }

}

QPixmap Icon::pressedPixmap(const QSize &size) const
{
    QPixmap pixmap;
    if(!size.isValid())
        pixmap = QPixmap::fromImage(mPressedImg);
    else {
        QImage retImg = mPressedImg;
        retImg = retImg.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        pixmap = QPixmap::fromImage(retImg);
    }
    return pixmap;
}
